<template>
  <el-container class="index">
    <el-header>
      <div class="logo">

       <router-link to="home"><img src="../assets/logo.png" alt /></router-link>

      </div>
      <div class="title">
        <h1>电商后台管理系统</h1>
      </div>
      <div class="logout">
        欢迎光临~
        <a @click="logout" href="javascript:;">退出</a>
      </div>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu
        router
         :default-active="defaultActive"
        unique-opened
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b">
          <!-- 子菜单 el-submenu -->
          <el-submenu :index="menu.path" v-for='menu in menuList' :key='menu.id'>
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{menu.authName}}</span>
            </template>
            <!-- 分组el-menu-item-group -->
            <el-menu-item :index="item.path" v-for="item in menu.children" :key='item.id'>
              <i class="el-icon-menu"></i>
              <span slot="title">{{item.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
          <!--路由出口 -->
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>

export default {
  computed: {
    defaultActive () {
    //   console.log(this.$route.path.slice(1))
      return this.$route.path.slice(1).split('-')[0]
    }
  },
  data () {
    return {
      menuList: []
    }
  },
  // 发送axios获取列表
  async created () {
    const { data, meta } = await this.$axios.get('menus', {

    })
    if (meta.status === 200) {
      this.menuList = data
      // console.log(this.menuList)
    } else {
      this.$message.error(meta.msg)
    }
  },

  methods: {
    logout () {
      this.$confirm('亲，你确认要进行退出吗？', '温馨提示', {
        type: 'warning'
      })
        .then(() => {
          // 退出成功的提示
          this.$message.success('退出成功')
          // 清除本地的token信息
          localStorage.removeItem('token')
          // 跳转到登录页
          this.$router.push('/login')
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>

<style lang="less" scopet>
.index {
  height: 100%;

  .el-header {
    background: #d8d8d8;
    display: flex;

    .logo {
      width: 180px;

      img {
        height: 40px;
        margin: 10px;
      }
    }
    .title {
      flex: 1;

      h1 {
        height: 60px;
        line-height: 60px;
        text-align: center;
        color: #545c64;
      }
    }
    .logout {
      width: 180px;
      text-align: right;
      font-weight: 700;
      height: 60px;
      line-height: 60px;

      a {
        color: orange;
      }
    }
  }
  .el-aside {
    background-color: #545c64;
    .el-menu{
        border-right: none;
    }
  }
  .el-main {
    background-color: #ecf0f1;
  }
}
</style>
